package com.zx.rbac.ums.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zx.rbac.common.api.IResultCode;
import com.zx.rbac.common.exception.RRException;
import com.zx.rbac.common.service.RedisService;
import com.zx.rbac.common.util.IdWorker;
import com.zx.rbac.common.util.MD5UtilQ;
import com.zx.rbac.common.util.TokenUtil;
import com.zx.rbac.ums.dao.UmsAdminDao;
import com.zx.rbac.ums.model.UmsAdminEntity;
import com.zx.rbac.ums.model.UmsMenuEntity;
import com.zx.rbac.ums.model.UmsRoleEntity;
import com.zx.rbac.ums.model.dto.UmsAdminDto;
import com.zx.rbac.ums.model.mapstruct.UmsAdminBeanMapper;
import com.zx.rbac.ums.service.UmsAdminService;
import com.zx.rbac.ums.service.UmsMenuService;
import com.zx.rbac.ums.service.UmsRoleService;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("umsAdminService")
public class UmsAdminServiceImpl  implements UmsAdminService {

    @Autowired
    private UmsAdminDao umsAdminDao;

    @Autowired
    private UmsRoleService umsRoleService;

    @Autowired
    private UmsMenuService umsMenuService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IdWorker idWorker;

    //private

    @Override
    public UmsAdminDto login(UmsAdminDto adminDto) {
        LambdaQueryWrapper<UmsAdminEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminEntity::getUsername,adminDto.getUsername());
        UmsAdminEntity umsAdminEntity = umsAdminDao.selectOne(queryWrapper);
        //验证用户
        if(ObjectUtils.defaultIfNull(umsAdminEntity,null)==null){
            throw new RRException(IResultCode.LOGIN_ERROR);
        }
        boolean verify = MD5UtilQ.verify(adminDto.getPassword(), umsAdminEntity.getPassword());
        if(!verify){
            throw new RRException(IResultCode.LOGIN_ERROR);
        }
        UmsAdminDto umsAdminDto = UmsAdminBeanMapper.INSTANCE.entityTODto(umsAdminEntity);
        //查询管理员所具有的角色
        List<UmsRoleEntity> roleEntities= umsRoleService.selectByAdminId(umsAdminEntity.getId());
        List<Long> collect = roleEntities.stream().map(UmsRoleEntity::getId).collect(Collectors.toList());
        umsAdminDto.setRoleIds(collect);
        //查询管理员所具有的管理菜单
        List<UmsMenuEntity> menuEntities = umsMenuService.selectByRoles(roleEntities);
        umsAdminDto.setMenus(menuEntities);
        //生成token
        String token = TokenUtil.createToken(umsAdminEntity.getId());
        //添加缓存
        redisService.set(token, JSON.toJSONString(umsAdminDto));
        return umsAdminDto;
    }

    @Override
    public void add(UmsAdminDto adminDto) {
        //TODO 判断用户名是否重复 这里先不做！
        String passwordMd5 = MD5UtilQ.MD5(adminDto.getPassword());
        UmsAdminEntity umsAdminEntity = UmsAdminBeanMapper.INSTANCE.dtoTOEntity(adminDto);
        umsAdminEntity.setPassword(passwordMd5);
        umsAdminEntity.setCreateTime(new Date());
        umsAdminEntity.setStatus(1);
        umsAdminEntity.setLevel(1);
        umsAdminEntity.setMemberId(String.valueOf(idWorker.nextId()));
        if(adminDto.getGender()==null){
            umsAdminEntity.setGender(0);
        }
        if(umsAdminDao.insert(umsAdminEntity)<=0){
            throw new RRException(IResultCode.FAILED);
        }
    }
}
